package com.boot.unity.api.wx.service;

import com.alibaba.fastjson.JSONObject;
import com.boot.unity.api.wx.utils.IpAddress;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class WxNewsService {

    @Value("${serv.servurl}")
    private String servurl;

    public Object queryNewsAll(String type) {
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/json");
        //设置请求头和数据
        RequestBody body = RequestBody.create(mediaType,type);
        //设置地址，方法，数据格式
        Request request = new Request.Builder()
                .url(servurl+ "/wx/news/queryNewsAll")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (IOException e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }

    public Object queryNewsById(String id) {
        OkHttpClient client = new OkHttpClient().newBuilder().build();
        MediaType mediaType = MediaType.parse("application/json");
        //设置请求头和数据
        RequestBody body = RequestBody.create(mediaType,id);
        //设置地址，方法，数据格式
        Request request = new Request.Builder()
                .url(servurl+ "/wx/news/queryNewsById")
                .method("POST", body)
                .addHeader("Content-Type", "application/json")
                .build();
        String strResult = "";
        try {
            Response response = client.newCall(request).execute();
            strResult = response.body().string();
            if (response.isSuccessful()) {
                // 接口调用成功
                JSONObject json = JSONObject.parseObject(strResult);
                return json;
            } else {
                //接口调用失败
                JSONObject resurt = new JSONObject();
                resurt.put("errno", "403");
                resurt.put("errmsg", "接口访问失败");
                return resurt;
            }
        } catch (IOException e) {
            e.printStackTrace();
            JSONObject resurt = new JSONObject();
            resurt.put("errno", "403");
            resurt.put("errmsg", e.getMessage());
            return resurt;
        }
    }
}
